/*
 * moto9s12.java
 *
 * Created on March 18, 2007, 8:43 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


package moto9s12sim;

import java.io.*;
import java.util.Scanner;
import java.lang.String;

/*
 *
 * @author Fergus Earnshaw
 *
 */



public class M9S12
{
    
    /** Creates a new instance of moto9s12 */

    static int iMemSize = 65536;
    //Registers;
    
    //A/B 8 bit static accumulator or D 16 bit double accumulator
   
    public Register8bt A;
    public Register8bt B;

        
    //X Index Register 16 bits
    
    public Register16bt X;
    
    //Y Index Register 16 bits
    
    public Register16bt Y;
    
    //SP Stack Pointer 16 bits
    
    public Register16bt SP;
    
    //PC Program Counter 16 bits
    
    public Register16bt PC;
    
    //CC Condition Code Register 8 bits
    
    public Register8btCC CC;
    
    private String strFile;
    public javax.swing.JTextPane tpConsole;
    
    //64kb Memory
    byte[] bMemory;
    
    
    private Instruction [] Command;  
    
    
    public M9S12()
    {
        
        bMemory = new byte[iMemSize];
        tpConsole = null;
        strFile = null;
        
        A   = new Register8bt();
        B   = new Register8bt();
        
        X   = new Register16bt();
        Y   = new Register16bt();
        SP  = new Register16bt();
        PC  = new Register16bt();        
        
        CC  = new Register8btCC();
        
        SP.setRegister((short)(iMemSize-1));                                    //STACK STARTS HIGH AND GOES LOW
        
        for (int i = 0; i < iMemSize; i++)
        {
            this.bMemory[i] = 0;
        }
        
        Command = new Instruction[255];
        //blank out instruction set
        for (int i = 0; i < 255; i++)   
        {
            Command[i] = new Instruction();
        }
        //Define instruction set for cpu
        
        //I only post 100% completed commands with a few exceptions as noted.
        
        Command[Integer.parseInt("00",16)] = new Instruction00();               //** Not Complete**//
        //Command[Integer.parseInt("01",16)] = new Instruction01();
        Command[Integer.parseInt("02",16)] = new Instruction02();
        Command[Integer.parseInt("03",16)] = new Instruction03();
        //Command[Integer.parseInt("04",16)] = new Instruction04(); *
        //Command[Integer.parseInt("05",16)] = new Instruction05(); *
        //Command[Integer.parseInt("06",16)] = new Instruction06(); *
        //Command[Integer.parseInt("07",16)] = new Instruction07(); *
        Command[Integer.parseInt("08",16)] = new Instruction08();
        Command[Integer.parseInt("09",16)] = new Instruction09();
        Command[Integer.parseInt("0A",16)] = new Instruction0A();
        Command[Integer.parseInt("0B",16)] = new Instruction0B();
        //Command[Integer.parseInt("0C",16)] = new Instruction0C(); *
        //Command[Integer.parseInt("0D",16)] = new Instruction0D(); *
        //Command[Integer.parseInt("0E",16)] = new Instruction0E(); *
        //Command[Integer.parseInt("0F",16)] = new Instruction0F(); *
        
        //Command[Integer.parseInt("10",16)] = new Instruction10(); *
        //Command[Integer.parseInt("11",16)] = new Instruction11();
        //Command[Integer.parseInt("12",16)] = new Instruction12();
        //Command[Integer.parseInt("13",16)] = new Instruction13();
        //Command[Integer.parseInt("14",16)] = new Instruction14(); *
        //Command[Integer.parseInt("15",16)] = new Instruction15(); *
        //Command[Integer.parseInt("16",16)] = new Instruction16(); *
        //Command[Integer.parseInt("17",16)] = new Instruction17(); *
        //Command[Integer.parseInt("18",16)] = new Instruction18(); *
        //Command[Integer.parseInt("19",16)] = new Instruction19(); *
        //Command[Integer.parseInt("1A",16)] = new Instruction1A(); *
        //Command[Integer.parseInt("1B",16)] = new Instruction1B(); *
        //Command[Integer.parseInt("1C",16)] = new Instruction1C(); *
        //Command[Integer.parseInt("1D",16)] = new Instruction1D(); *
        //Command[Integer.parseInt("1E",16)] = new Instruction1E(); *
        //Command[Integer.parseInt("1F",16)] = new Instruction1F(); *
        
        //Command[Integer.parseInt("20",16)] = new Instruction20(); *
        //Command[Integer.parseInt("21",16)] = new Instruction21(); *
        //Command[Integer.parseInt("22",16)] = new Instruction22(); *
        //Command[Integer.parseInt("23",16)] = new Instruction23(); *
        //Command[Integer.parseInt("24",16)] = new Instruction24(); *
        //Command[Integer.parseInt("25",16)] = new Instruction25(); *
        //Command[Integer.parseInt("26",16)] = new Instruction26(); *
        //Command[Integer.parseInt("27",16)] = new Instruction27(); *
        //Command[Integer.parseInt("28",16)] = new Instruction28(); *
        //Command[Integer.parseInt("29",16)] = new Instruction29(); *
        //Command[Integer.parseInt("2A",16)] = new Instruction2A(); *
        //Command[Integer.parseInt("2B",16)] = new Instruction2B(); *
        //Command[Integer.parseInt("2C",16)] = new Instruction2C(); *
        //Command[Integer.parseInt("2D",16)] = new Instruction2D(); *
        //Command[Integer.parseInt("2E",16)] = new Instruction2E(); *
        //Command[Integer.parseInt("2F",16)] = new Instruction2F(); *
        
        Command[Integer.parseInt("30",16)] = new Instruction30();               //PULX
        Command[Integer.parseInt("31",16)] = new Instruction31();               //PULY
        Command[Integer.parseInt("32",16)] = new Instruction32();               //PULA
        Command[Integer.parseInt("33",16)] = new Instruction33();               //PULB
        //Command[Integer.parseInt("34",16)] = new Instruction34();
        //Command[Integer.parseInt("35",16)] = new Instruction35();
        Command[Integer.parseInt("36",16)] = new Instruction36();               //PSHA
        Command[Integer.parseInt("37",16)] = new Instruction37();               //PSHB 
        //Command[Integer.parseInt("38",16)] = new Instruction38();
        Command[Integer.parseInt("39",16)] = new Instruction39();               //PSHC
        Command[Integer.parseInt("3A",16)] = new Instruction3A();               //PULD
        Command[Integer.parseInt("3B",16)] = new Instruction3B();               //PSHD
        //Command[Integer.parseInt("3C",16)] = new Instruction3C();
        Command[Integer.parseInt("3D",16)] = new Instruction3D();               //RTS
        //Command[Integer.parseInt("3E",16)] = new Instruction3E();
        //Command[Integer.parseInt("3F",16)] = new Instruction3F();
        
        Command[Integer.parseInt("40",16)] = new Instruction40();               //NEGA
        Command[Integer.parseInt("41",16)] = new Instruction41();               //COMA
        Command[Integer.parseInt("42",16)] = new Instruction42();               //INCA
        Command[Integer.parseInt("43",16)] = new Instruction43();               //DECA
        //Command[Integer.parseInt("44",16)] = new Instruction44();
        //Command[Integer.parseInt("45",16)] = new Instruction45();
        //Command[Integer.parseInt("46",16)] = new Instruction46();
        //Command[Integer.parseInt("47",16)] = new Instruction47();
        //Command[Integer.parseInt("48",16)] = new Instruction48();
        //Command[Integer.parseInt("49",16)] = new Instruction49();
        //Command[Integer.parseInt("4A",16)] = new Instruction4A(); *
        //Command[Integer.parseInt("4B",16)] = new Instruction4B(); *
        //Command[Integer.parseInt("4C",16)] = new Instruction4C(); *
        //Command[Integer.parseInt("4D",16)] = new Instruction4D(); *
        //Command[Integer.parseInt("4E",16)] = new Instruction4E(); *
        //Command[Integer.parseInt("4F",16)] = new Instruction4F(); *
        
        Command[Integer.parseInt("50",16)] = new Instruction50();               //NEGB
        Command[Integer.parseInt("51",16)] = new Instruction51();               //COMB
        Command[Integer.parseInt("52",16)] = new Instruction52();               //INCB
        Command[Integer.parseInt("53",16)] = new Instruction53();               //DECB
        //Command[Integer.parseInt("54",16)] = new Instruction54();
        Command[Integer.parseInt("55",16)] = new Instruction55();               //ROLB
        //Command[Integer.parseInt("56",16)] = new Instruction56();
        //Command[Integer.parseInt("57",16)] = new Instruction57();
        //Command[Integer.parseInt("58",16)] = new Instruction58();
        //Command[Integer.parseInt("59",16)] = new Instruction59();
        //Command[Integer.parseInt("5A",16)] = new Instruction5A(); *
        //Command[Integer.parseInt("5B",16)] = new Instruction5B(); *
        //Command[Integer.parseInt("5C",16)] = new Instruction5C(); *
        //Command[Integer.parseInt("5D",16)] = new Instruction5D(); *
        //Command[Integer.parseInt("5E",16)] = new Instruction5E(); *
        //Command[Integer.parseInt("5F",16)] = new Instruction5F(); *

        //Command[Integer.parseInt("60",16)] = new Instruction60(); *
        //Command[Integer.parseInt("61",16)] = new Instruction61(); *
        //Command[Integer.parseInt("62",16)] = new Instruction62(); *
        //Command[Integer.parseInt("63",16)] = new Instruction63(); *
        //Command[Integer.parseInt("64",16)] = new Instruction64(); *
        //Command[Integer.parseInt("65",16)] = new Instruction65(); *
        //Command[Integer.parseInt("66",16)] = new Instruction66(); *
        //Command[Integer.parseInt("67",16)] = new Instruction67(); *
        //Command[Integer.parseInt("68",16)] = new Instruction68(); *
        //Command[Integer.parseInt("69",16)] = new Instruction69(); *
        //Command[Integer.parseInt("6A",16)] = new Instruction6A(); *
        //Command[Integer.parseInt("6B",16)] = new Instruction6B(); *
        //Command[Integer.parseInt("6C",16)] = new Instruction6C(); *
        //Command[Integer.parseInt("6D",16)] = new Instruction6D(); *
        //Command[Integer.parseInt("6E",16)] = new Instruction6E(); *
        //Command[Integer.parseInt("6F",16)] = new Instruction6F(); *
        
        //Command[Integer.parseInt("70",16)] = new Instruction70(); *
        //Command[Integer.parseInt("71",16)] = new Instruction71(); *
        //Command[Integer.parseInt("72",16)] = new Instruction72(); *
        //Command[Integer.parseInt("73",16)] = new Instruction73(); *
        //Command[Integer.parseInt("74",16)] = new Instruction74(); *
        //Command[Integer.parseInt("75",16)] = new Instruction75(); *
        //Command[Integer.parseInt("76",16)] = new Instruction76(); *
        //Command[Integer.parseInt("77",16)] = new Instruction77(); *
        //Command[Integer.parseInt("78",16)] = new Instruction78(); *
        //Command[Integer.parseInt("79",16)] = new Instruction79(); *
        //Command[Integer.parseInt("7A",16)] = new Instruction7A(); *
        //Command[Integer.parseInt("7B",16)] = new Instruction7B(); *
        //Command[Integer.parseInt("7C",16)] = new Instruction7C(); *
        //Command[Integer.parseInt("7D",16)] = new Instruction7D(); *
        //Command[Integer.parseInt("7E",16)] = new Instruction7E(); *
        //Command[Integer.parseInt("7F",16)] = new Instruction7F(); *
        
        //Command[Integer.parseInt("80",16)] = new Instruction80(); *
        //Command[Integer.parseInt("81",16)] = new Instruction81(); *
        //Command[Integer.parseInt("82",16)] = new Instruction82(); *
        //Command[Integer.parseInt("83",16)] = new Instruction83(); *
        //Command[Integer.parseInt("84",16)] = new Instruction84(); *
        //Command[Integer.parseInt("85",16)] = new Instruction85(); *
        //Command[Integer.parseInt("86",16)] = new Instruction86(); *
        Command[Integer.parseInt("87",16)] = new Instruction87();               //CLRA
        //Command[Integer.parseInt("88",16)] = new Instruction88(); *
        //Command[Integer.parseInt("89",16)] = new Instruction89(); *
        //Command[Integer.parseInt("8A",16)] = new Instruction8A(); *
        //Command[Integer.parseInt("8B",16)] = new Instruction8B(); *
        //Command[Integer.parseInt("8C",16)] = new Instruction8C(); *
        //Command[Integer.parseInt("8D",16)] = new Instruction8D(); *
        //Command[Integer.parseInt("8E",16)] = new Instruction8E(); *
        //Command[Integer.parseInt("8F",16)] = new Instruction8F(); *
        
        //Command[Integer.parseInt("90",16)] = new Instruction90(); *
        //Command[Integer.parseInt("91",16)] = new Instruction91(); *
        //Command[Integer.parseInt("92",16)] = new Instruction92(); *
        //Command[Integer.parseInt("93",16)] = new Instruction93(); *
        //Command[Integer.parseInt("94",16)] = new Instruction94(); *
        //Command[Integer.parseInt("95",16)] = new Instruction95(); *
        //Command[Integer.parseInt("96",16)] = new Instruction96(); *
        Command[Integer.parseInt("97",16)] = new Instruction97();               //TSTA
        //Command[Integer.parseInt("98",16)] = new Instruction98(); *
        //Command[Integer.parseInt("99",16)] = new Instruction99(); *
        //Command[Integer.parseInt("9A",16)] = new Instruction9A(); *
        //Command[Integer.parseInt("9B",16)] = new Instruction9B(); *
        //Command[Integer.parseInt("9C",16)] = new Instruction9C(); *
        //Command[Integer.parseInt("9D",16)] = new Instruction9D(); *
        //Command[Integer.parseInt("9E",16)] = new Instruction9E(); *
        //Command[Integer.parseInt("9F",16)] = new Instruction9F(); *
        
        //Command[Integer.parseInt("A0",16)] = new InstructionA0(); *
        //Command[Integer.parseInt("A1",16)] = new InstructionA1(); *
        //Command[Integer.parseInt("A2",16)] = new InstructionA2(); *
        //Command[Integer.parseInt("A3",16)] = new InstructionA3(); *
        //Command[Integer.parseInt("A4",16)] = new InstructionA4(); *
        //Command[Integer.parseInt("A5",16)] = new InstructionA5(); *
        //Command[Integer.parseInt("A6",16)] = new InstructionA6(); *
        Command[Integer.parseInt("A7",16)] = new InstructionA7();               //NOP
        //Command[Integer.parseInt("A8",16)] = new InstructionA8(); *
        //Command[Integer.parseInt("A9",16)] = new InstructionA9(); *
        //Command[Integer.parseInt("AA",16)] = new InstructionAA(); *
        //Command[Integer.parseInt("AB",16)] = new InstructionAB(); *
        //Command[Integer.parseInt("AC",16)] = new InstructionAC(); *
        //Command[Integer.parseInt("AD",16)] = new InstructionAD(); *
        //Command[Integer.parseInt("AE",16)] = new InstructionAE(); *
        //Command[Integer.parseInt("AF",16)] = new InstructionAF(); *

        //Command[Integer.parseInt("B0",16)] = new InstructionB0(); *
        //Command[Integer.parseInt("B1",16)] = new InstructionB1(); *
        //Command[Integer.parseInt("B2",16)] = new InstructionB2(); *
        //Command[Integer.parseInt("B3",16)] = new InstructionB3(); *
        //Command[Integer.parseInt("B4",16)] = new InstructionB4(); *
        //Command[Integer.parseInt("B5",16)] = new InstructionB5(); *
        //Command[Integer.parseInt("B6",16)] = new InstructionB6(); *
        //Command[Integer.parseInt("B7",16)] = new InstructionB7(); *
        //Command[Integer.parseInt("B8",16)] = new InstructionB8(); *
        //Command[Integer.parseInt("B9",16)] = new InstructionB9(); *
        //Command[Integer.parseInt("BA",16)] = new InstructionBA(); *
        //Command[Integer.parseInt("BB",16)] = new InstructionBB(); *
        //Command[Integer.parseInt("BC",16)] = new InstructionBC(); *
        //Command[Integer.parseInt("BD",16)] = new InstructionBD(); *
        //Command[Integer.parseInt("BE",16)] = new InstructionBE(); *
        //Command[Integer.parseInt("BF",16)] = new InstructionBF(); *

        //Command[Integer.parseInt("C0",16)] = new InstructionC0(); *
        //Command[Integer.parseInt("C1",16)] = new InstructionC1(); *
        //Command[Integer.parseInt("C2",16)] = new InstructionC2(); *
        //Command[Integer.parseInt("C3",16)] = new InstructionC3(); *
        //Command[Integer.parseInt("C4",16)] = new InstructionC4(); *
        //Command[Integer.parseInt("C5",16)] = new InstructionC5(); *
        //Command[Integer.parseInt("C6",16)] = new InstructionC6(); *
        Command[Integer.parseInt("C7",16)] = new InstructionC7();               //CLRB
        //Command[Integer.parseInt("C8",16)] = new InstructionC8(); *
        //Command[Integer.parseInt("C9",16)] = new InstructionC9(); *
        //Command[Integer.parseInt("CA",16)] = new InstructionCA(); *
        //Command[Integer.parseInt("CB",16)] = new InstructionCB(); *
        //Command[Integer.parseInt("CC",16)] = new InstructionCC(); *
        //Command[Integer.parseInt("CD",16)] = new InstructionCD(); *
        //Command[Integer.parseInt("CE",16)] = new InstructionCE(); *
        //Command[Integer.parseInt("CF",16)] = new InstructionCF(); *

        //Command[Integer.parseInt("D0",16)] = new InstructionD0(); *
        //Command[Integer.parseInt("D1",16)] = new InstructionD1(); *
        //Command[Integer.parseInt("D2",16)] = new InstructionD2(); *
        //Command[Integer.parseInt("D3",16)] = new InstructionD3(); *
        //Command[Integer.parseInt("D4",16)] = new InstructionD4(); *
        //Command[Integer.parseInt("D5",16)] = new InstructionD5(); *
        //Command[Integer.parseInt("D6",16)] = new InstructionD6(); *
        Command[Integer.parseInt("D7",16)] = new InstructionD7();               //TSTB
        //Command[Integer.parseInt("D8",16)] = new InstructionD8(); *
        //Command[Integer.parseInt("D9",16)] = new InstructionD9(); *
        //Command[Integer.parseInt("DA",16)] = new InstructionDA(); *
        //Command[Integer.parseInt("DB",16)] = new InstructionDB(); *
        //Command[Integer.parseInt("DC",16)] = new InstructionDC(); *
        //Command[Integer.parseInt("DD",16)] = new InstructionDD(); *
        //Command[Integer.parseInt("DE",16)] = new InstructionDE(); *
        //Command[Integer.parseInt("DF",16)] = new InstructionDF(); *

        //Command[Integer.parseInt("E0",16)] = new InstructionE0(); *
        //Command[Integer.parseInt("E1",16)] = new InstructionE1(); *
        //Command[Integer.parseInt("E2",16)] = new InstructionE2(); *
        //Command[Integer.parseInt("E3",16)] = new InstructionE3(); *
        //Command[Integer.parseInt("E4",16)] = new InstructionE4(); *
        //Command[Integer.parseInt("E5",16)] = new InstructionE5(); *
        //Command[Integer.parseInt("E6",16)] = new InstructionE6(); *
        //Command[Integer.parseInt("E7",16)] = new InstructionE7(); *
        //Command[Integer.parseInt("E8",16)] = new InstructionE8(); *
        //Command[Integer.parseInt("E9",16)] = new InstructionE9(); *
        //Command[Integer.parseInt("EA",16)] = new InstructionEA(); *
        //Command[Integer.parseInt("EB",16)] = new InstructionEB(); *
        //Command[Integer.parseInt("EC",16)] = new InstructionEC(); *
        //Command[Integer.parseInt("ED",16)] = new InstructionED(); *
        //Command[Integer.parseInt("EE",16)] = new InstructionEE(); *
        //Command[Integer.parseInt("EF",16)] = new InstructionEF(); *
       
        
        //Command[Integer.parseInt("F0",16)] = new InstructionF0(); *
        //Command[Integer.parseInt("F1",16)] = new InstructionF1(); *
        //Command[Integer.parseInt("F2",16)] = new InstructionF2(); *
        //Command[Integer.parseInt("F3",16)] = new InstructionF3(); *
        //Command[Integer.parseInt("F4",16)] = new InstructionF4(); *
        //Command[Integer.parseInt("F5",16)] = new InstructionF5(); *
        //Command[Integer.parseInt("F6",16)] = new InstructionF6(); *
        //Command[Integer.parseInt("F7",16)] = new InstructionF7(); *
        //Command[Integer.parseInt("F8",16)] = new InstructionF8(); *
        //Command[Integer.parseInt("F9",16)] = new InstructionF9(); *
        //Command[Integer.parseInt("FA",16)] = new InstructionFA(); *
        //Command[Integer.parseInt("FB",16)] = new InstructionFB(); *
        //Command[Integer.parseInt("FC",16)] = new InstructionFC(); *
        //Command[Integer.parseInt("FD",16)] = new InstructionFD(); *
        //Command[Integer.parseInt("FE",16)] = new InstructionFE(); *
        //Command[Integer.parseInt("FF",16)] = new InstructionFF(); *
    }
    
    public void startSimulation()
    {
        this.writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");
        this.writelnToConsole("Location\tCodes\t\t\tAssembly Code");
        this.writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");        
    }
    
    public void stopSimulation()
    {
        this.writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");        
    }
    
    public boolean simulate()
    {        
        return Command[(this.bMemory[this.PC.getUnsignedRegister()] >= 0) ? this.bMemory[this.PC.getUnsignedRegister()] : 256 + this.bMemory[this.PC.getUnsignedRegister()]].excute(this);
    }
    
    public boolean loadHEXFile ()
    {
        return loadHEXFile(this.strFile);
    }
    
    public boolean loadHEXFile(String strFileName) //throws IOException
    {
        //Declare Variables
        
        Scanner s = null;
        String SRec;
        
        SRecord current;
        this.strFile = strFileName;
        writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");
        writelnToConsole("Loading File...");
        
        writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");
      
        try
        {
            File file = new File(strFileName);
            s = new Scanner(file);
            
            while (s.hasNext())
            {
                SRec = s.next();     
                current = new SRecord(SRec);
                current.processSRecord(this);                
            }
            writelnToConsole("File Loaded...");
            writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");
        }
        
        catch (FileNotFoundException e)
        {
            writelnToConsole("Error: Unable to Load File");
            writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");
            return false;
        }
    
        finally {
            if (s != null) {
                s.close();
            }
        }
        
        return true;
    }
    public void writeToConsole(String strTmp)
    {
        if (this.tpConsole == null)
        {
            System.out.print(strTmp);
        }
        else 
        {
            this.tpConsole.setText(this.tpConsole.getText() + strTmp);
        }
    }
    public void writelnToConsole(String strTmp)
    {
        if (this.tpConsole == null)
        {
            System.out.println(strTmp);
        }
        else 
        {
            this.tpConsole.setText(this.tpConsole.getText() + strTmp + "\r\n");
        }
    }
    public void printMemoryDump()
    {
        this.writelnToConsole("MEMORY DUMP");
        this.writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");
        this.writelnToConsole("Address\tData");
        this.writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");
        
        for (int i = 0; i < bMemory.length; i++)
        {
            if (bMemory[i] != 0)
            {               
               this.writelnToConsole(this.byteToHex((byte)i) + " \t " + this.byteToHex(bMemory[i]) );
            }
        }
        
        this.writelnToConsole("------------------------------------------------------------------------------------------------------------------------------------------");
        
    }
    
    public void printState()
    {
                
        this.writelnToConsole("-------------------------------------------------------------------");
        this.writelnToConsole("Current CPU Register State");
        this.writelnToConsole("-------------------------------------------------------------------");
        this.writelnToConsole("A:  " + A.getHex()  + "   (" + A.getUnsignedRegister()  + ")");
        this.writelnToConsole("B:  " + B.getHex()  + "   (" + B.getUnsignedRegister()  + ")");
        this.writelnToConsole("X:  " + X.getHex()  + " (" + X.getUnsignedRegister()  + ")");
        this.writelnToConsole("Y:  " + Y.getHex()  + " (" + Y.getUnsignedRegister()  + ")");
        this.writelnToConsole("SP: " + SP.getHex() + " (" + SP.getUnsignedRegister() + ")");
        this.writelnToConsole("PC: " + PC.getHex() + " (" + PC.getUnsignedRegister() + ")");
        this.writelnToConsole("-------------------------------------------------------------------");
        this.writelnToConsole("Current Flags State");
        this.writelnToConsole("-------------------------------------------------------------------");        
        this.writelnToConsole("CC-S: " + CC.printStatusS() +" (STOP DISABLE)");        
        this.writelnToConsole("CC-X: " + CC.printStatusX() +" (MASK)");
        this.writelnToConsole("CC-H: " + CC.printStatusH() +" (HALF-CARRY)");
        this.writelnToConsole("CC-I: " + CC.printStatusI() +" (MASK DISABLE IRQ)");
        this.writelnToConsole("CC-N: " + CC.printStatusN() +" (NEGITIVE)");
        this.writelnToConsole("CC-Z: " + CC.printStatusZ() +" (ZERO)");
        this.writelnToConsole("CC-V: " + CC.printStatusV() +" (OVERFLOW)");
        this.writelnToConsole("CC-C: " + CC.printStatusC() +" (CARRY)");
        this.writelnToConsole("-------------------------------------------------------------------");
    
    }

    public void setConsole(javax.swing.JTextPane tpTmpConsole) {        
        tpConsole = tpTmpConsole;
    }
    
    private String shortToHex(short sTmp)
    {
        String strTmp;
        
        strTmp = Integer.toHexString((sTmp >= 0) ? sTmp : 65536 + sTmp).toUpperCase();
        
        while(strTmp.length() < 4)
        {
            strTmp = "0" + strTmp;
        }
        
        return strTmp;
    }
    
    private String byteToHex(byte bTmp)
    {
        String strTmp;
        
        strTmp = Integer.toHexString((bTmp >= 0) ? bTmp : 256 + bTmp).toUpperCase();
        
        while(strTmp.length() < 2)
        {
            strTmp = "0" + strTmp;
        }
        
        return strTmp;
    }

    public String [] getMemory(boolean [] blBreakPoints){
        String [] strings;
        
        strings = new String[100];//I only think the next 100 instructions are needed. //if more are needed the dump state will allow the user to dump the entire contents.
        
        int i = 0;
        int index;
      
        while (i < strings.length)
        {
            
            index = PC.getUnsignedRegister()+i;
            
            if (index >= iMemSize)
            {
                index -= iMemSize;
            }
            strings[i] = "["+shortToHex((short)index)+"]    |    ["+byteToHex((byte)bMemory[index])+"]";
            if (blBreakPoints[index])
            {
                strings[i] = strings[i] + " * BP * ";
            }
            i++;
        }
        
        return strings;
    }
    
    public String [] getStack(boolean [] blStackPoints){
        
        String [] strings;
        
        strings = new String[24];
        
        int i = 0;
        int index;
      
        while (i < strings.length)
        {
            
            index = SP.getUnsignedRegister()+i;
            
            if (index >= iMemSize)
            {
                index -= iMemSize;
            }
            strings[i] = "["+shortToHex((short)index)+"]    |    ["+byteToHex((byte)bMemory[index])+"]";
            
            if (blStackPoints[index])
            {
                strings[i-1] = strings[i-1] + " * SP * ";
            }
            i++;
        }
        
        return strings;
        
    }
    
   
}
